package com.study.web.nv.util;

import lombok.extern.slf4j.Slf4j;
import nvr.utils.NvrUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author study
 * @version 1.0
 * @date 2020/8/9 15:42
 */
@Slf4j
public class ParseNs {
    public void parseGallery() throws IOException {
        //listdiv
        String resourcePath = NvrUtils.getResourcePath("hs.html");
        System.out.println(resourcePath);


    }

    /**
     * https://www.nvshens.org/girl/23100/
     *
     * @param url
     * @param base
     * @return
     */
    public static List<String> parseAlbum(String url, String base) {
        List<String> albumList = new ArrayList<>();
        Document root = get(url);
        if (root == null) {
            return albumList;
        }
        Elements albumBase = root.select(".archive_more a");
        String digits = StringUtils.getDigits(albumBase.text());

        String href = albumBase.attr("href");
        int albumNum = Integer.parseInt(digits);
        albumNum = (albumNum + 29) / 30;//页码数

        for (int i = 1; i <= albumNum; i++) {
            String albumPageUrl = "";
            if (i == 1) {
                albumPageUrl = base + href;
            } else {
                albumPageUrl = String.format("%s%s%s.html", base, href, i);
            }
            Document document = get(albumPageUrl);
            if (document == null) {
                return albumList;
            }
            Elements elePics = document.select(".igalleryli_link");
            albumList.addAll(elePics.stream().map(
                    ele -> ele.attr("href"))
                    .collect(Collectors.toList()));
        }
        return albumList;
    }

    private void parse() {

    }

    public static Document get(String url) {
        Document root = null;
        try {
            root = Jsoup.connect(url).timeout((int) TimeUnit.SECONDS.toMillis(50)).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return root;
    }

    public static void main(String[] args) throws IOException {
        String path = NvrUtils.getResourcePath("hs.html");
        Document root = Jsoup.parse(FileUtils.readFileToString(new File(path)));

        Elements ePageNum = root.select(".pagesYY");

        System.out.println(ePageNum.text());
        System.out.println("-----------------");
        System.out.println(ePageNum.html());

        Elements picGarrly = root.select(".galleryli_link");
        System.out.println(picGarrly);


        List<String> collects = picGarrly.stream().map(ele -> ele.attr("href")).collect(Collectors.toList());

        System.out.println(collects);

        List<String> list = Arrays.asList("a", "b", "c");
        for (String s : list) {
            if (s.equals("b")) {
                log.info("存在了{}", s);
                continue;
            }
            log.info(s);
        }

    }
}
